#-*- encoding: utf-8 -*-
import win32com.client 
from Misc.Utils import *

from DataAccess.QueryBlp import *

from datetime import date,timedelta

from ReportTool.HldReport import *
from ReportTool.PnLReport import *
from ReportTool.NAVReport import *
from ReportTool.MktValReport import *
from ReportTool.TradeReport import *
from ReportTool.RealizedPnL import *
from ReportTool.OnshoreHldReport import *

from Config.ReportConfig import *	
		
def Monthly_Region_Expo_And_Attr(ref_date):
	
	tplt_file = TPLT_PATH + 'Monthly Region Exposure And Attribution.xls'
	xApp = win32com.client.Dispatch("Excel.Application")
	xBook = xApp.Workbooks.Open(tplt_file)
	xSht = xBook.Worksheets('Report')
	
	d = date(2007, 10 ,31)
	row = 3
	
	while d <= ref_date:
	
		hld = query_hld(d, portf_list=['SC028'])
		pnl = query_pnl(date(d.year,d.month,1), d, portf_list=['SC028'])
		region_expo = {}
		region_ret = {}
		
		for elem in hld:
			tick = elem[0]
			
			if elem[2] == 'CASH' or elem[2] == 'REPO':
				continue
			elif elem[2] == 'PNOTE':
				sec_region = 'B' if (tick[0] == '2' or tick[0] == '9') else 'A'			
			elif (elem[2] == 'STOCK' and elem[17] == 'HKD'):
				sec_region = 'H'
			elif elem[2] == 'WARRANT':
				sec_region = 'W'
			else:
				sec_region = 'Others'
			
			if region_expo.has_key((sec_region, elem[3])):
				region_expo[(sec_region, elem[3])] += elem[12]
			else:
				region_expo[(sec_region, elem[3])] = elem[12]

		for elem in pnl:
			tick = elem[0]
			
			if elem[2] == 'CASH' or elem[2] == 'REPO':
				continue
			elif elem[2] == 'PNOTE':
				sec_region = 'B' if (tick[0] == '2' or tick[0] == '9') else 'A'			
			elif (elem[2] == 'STOCK' and elem[7] == 'HKD'):
				sec_region = 'H'
			elif elem[2] == 'WARRANT':
				sec_region = 'W'
			else:
				sec_region = 'Others'
			
			if region_ret.has_key((sec_region, elem[3])):
				region_ret[(sec_region, elem[3])] += elem[6]
			else:
				region_ret[(sec_region, elem[3])] = elem[6]				
				
		xSht.Cells(row, 1).Value = d.isoformat()		
		xSht.Cells(row, 2).Value = region_expo[('A', 'LONG')] if region_expo.has_key(('A', 'LONG')) else 0
		xSht.Cells(row, 3).Value = region_expo[('B', 'LONG')] if region_expo.has_key(('B', 'LONG')) else 0
		xSht.Cells(row, 4).Value = region_expo[('H', 'LONG')] if region_expo.has_key(('H', 'LONG')) else 0
		xSht.Cells(row, 5).Value = region_expo[('H', 'SHORT')] if region_expo.has_key(('H', 'SHORT')) else 0
		xSht.Cells(row, 6).Value = region_expo[('W', 'LONG')] if region_expo.has_key(('W', 'LONG')) else 0
		xSht.Cells(row, 7).Value = region_expo[('Others', 'LONG')] if region_expo.has_key(('Others', 'LONG')) else 0
		xSht.Cells(row, 8).Value = region_expo[('Others', 'SHORT')] if region_expo.has_key(('Others', 'SHORT')) else 0
		xSht.Cells(row, 9).Value = region_ret[('A', 'LONG')] if region_ret.has_key(('A', 'LONG')) else 0
		xSht.Cells(row, 10).Value = region_ret[('B', 'LONG')] if region_ret.has_key(('B', 'LONG')) else 0
		xSht.Cells(row, 11).Value = region_ret[('H', 'LONG')] if region_ret.has_key(('H', 'LONG')) else 0
		xSht.Cells(row, 12).Value = region_ret[('H', 'SHORT')] if region_ret.has_key(('H', 'SHORT')) else 0
		xSht.Cells(row, 13).Value = region_ret[('W', 'LONG')] if region_ret.has_key(('W', 'LONG')) else 0		
		xSht.Cells(row, 14).Value = region_ret[('Others', 'LONG')] if region_ret.has_key(('Others', 'LONG')) else 0
		xSht.Cells(row, 15).Value = region_ret[('Others', 'SHORT')] if region_ret.has_key(('Others', 'SHORT')) else 0
		
		print '   running info, region exposure and attribution completed at ', d.isoformat()
		d = end_of_mth(d, 1)
		row += 1
			
	rept_file = REPT_PATH + 'Monthly Region Exposure And Attribution_' + ref_date.strftime("%Y%m%d") + '.xls'
	xBook.SaveAs(rept_file)
	xBook.Close()
	del xApp
	
	print 'info, monthly region exposure and attribution report completed at ' , ref_date.isoformat()